
            <!DOCTYPE html>
            <html lang="en">
            <head>
                <meta charset="UTF-8">
                <title>[k8s] 第九章 安全认证 RBAC</title>
            </head>
            <body>
            <a href="https://andyoung.blog.csdn.net">原作者博客</a>
            <div id="content_views" class="markdown_views prism-atom-one-light">
                    <svg xmlns="http://www.w3.org/2000/svg" style="display: none;">
                        <path stroke-linecap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-block" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"></path>
                    </svg>
                    <ul><li>本章节主要介绍Kubernetes的安全认证机制。</li></ul> 
<h3><a id="_2"></a>访问控制概述</h3> 
<p><code>Kubernetes</code>作为一个分布式集群的管理工具，保证集群的安全性是其一个重要的任务。所谓的安全性其实就是保证对Kubernetes的各种<strong>客户端对资源访问进行认证和鉴权</strong>操作。</p> 
<h3><a id="RBAC_API__6"></a>RBAC API 对象</h3> 
<p><code>Kubernetes</code>有一个很基本的特性就是它的<a href="https://kubernetes.io/docs/concepts/overview/working-with-objects/kubernetes-objects/" rel="nofollow">所有资源对象都是模型化的 API 对象</a>，允许执行 CRUD(Create、Read、Update、Delete)操作(也就是我们常说的增、删、改、查操作)，比如下面的这下资源：</p> 
<ul><li>Pods</li><li>ConfigMaps</li><li>Deployments</li><li>Nodes</li><li>Secrets</li><li>Namespaces</li></ul> 
<p>上面这些资源对象的可能存在的操作有：</p> 
<ul><li>create</li><li>get</li><li>delete</li><li>list</li><li>update</li><li>edit</li><li>watch</li><li>exec</li></ul> 
<p>在更上层，这些资源和 API Group 进行关联，比如<code>Pods</code>属于 Core API Group，而<code>Deployements</code>属于 apps API Group，要在<code>Kubernetes</code>中进行<code>RBAC</code>的管理，除了上面的这些资源和操作以外，我们还需要另外的一些对象：</p> 
<ul><li>Rule：规则，规则是一组属于不同 API Group 资源上的一组操作的集合</li><li>Role 和 ClusterRole：角色和集群角色，这两个对象都包含上面的 Rules 元素，二者的区别在于，在 Role 中，定义的规则只适用于单个命名空间，也就是和 namespace 关联的，而 ClusterRole 是集群范围内的，因此定义的规则不受命名空间的约束。另外 Role 和 ClusterRole 在<code>Kubernetes</code>中都被定义为集群内部的 API 资源，和我们前面学习过的 Pod、ConfigMap 这些类似，都是我们集群的资源对象，所以同样的可以使用我们前面的<code>kubectl</code>相关的命令来进行操作</li><li>Subject：主题，对应在集群中尝试操作的对象，集群中定义了3种类型的主题资源： 
  <ul><li>User Account：用户，这是有外部独立服务进行管理的，管理员进行私钥的分配，用户可以使用 KeyStone或者 Goolge 帐号，甚至一个用户名和密码的文件列表也可以。对于用户的管理集群内部没有一个关联的资源对象，所以用户不能通过集群内部的 API 来进行管理</li><li>Group：组，这是用来关联多个账户的，集群中有一些默认创建的组，比如cluster-admin</li><li>Service Account：服务帐号，通过<code>Kubernetes</code> API 来管理的一些用户帐号，和 namespace 进行关联的，适用于集群内部运行的应用程序，需要通过 API 来完成权限认证，所以在集群内部进行权限操作，我们都需要使用到 ServiceAccount，这也是我们这节课的重点</li></ul> </li><li>RoleBinding 和 ClusterRoleBinding：角色绑定和集群角色绑定，简单来说就是把声明的 Subject 和我们的 Role 进行绑定的过程(给某个用户绑定上操作的权限)，二者的区别也是作用范围的区别：RoleBinding 只会影响到当前 namespace 下面的资源操作权限，而 ClusterRoleBinding 会影响到所有的 namespace。</li></ul> 
<h3><a id="_40"></a><strong>客户端</strong></h3> 
<p>在Kubernetes集群中，客户端通常有两类：</p> 
<ul><li> <p><strong>User Account</strong>：一般是独立于kubernetes之外的其他服务管理的用户账号。</p> </li><li> <p><strong>Service Account</strong>：kubernetes管理的账号，用于为Pod中的服务进程在访问Kubernetes时提供身份标识。</p> </li></ul> 
<p><img src="https://i-blog.csdnimg.cn/blog_migrate/1b189a9089b48970dafabb3ac8d6d196.png#pic_center" alt=""></p> 
<p>Service account是为了方便Pod里面的进程调用Kubernetes API或其他外部服务而设计的。它与User account不同</p> 
<ul><li>User account是为人设计的，而service account则是为Pod中的进程调用Kubernetes API而设计；</li><li>User account是跨namespace的，而service account则是仅局限它所在的namespace；</li><li>每个namespace都会自动创建一个default service account</li><li>Token controller检测service account的创建，并为它们创建secret</li><li>开启ServiceAccount Admission Controller后 
  <ul><li>每个Pod在创建后都会自动设置spec.serviceAccount为default（除非指定了其他ServiceAccout）</li><li>验证Pod引用的service account已经存在，否则拒绝创建</li><li>如果Pod没有指定ImagePullSecrets，则把service account的ImagePullSecrets加到Pod中</li><li>每个container启动后都会挂载该service account的token和ca.crt到/var/run/secrets/kubernetes.io/serviceaccount/</li></ul> </li></ul> 
<blockquote> 
 <p>当创建 pod 的时候，如果没有指定一个 service account，系统会自动在与该pod 相同的 namespace 下为其指派一个default service account。而pod和apiserver之间进行通信的账号，称为serviceAccountName。</p> 
</blockquote> 
<p><strong>认证、授权与准入控制</strong></p> 
<p>ApiServer是访问及管理资源对象的唯一入口。任何一个请求访问ApiServer，都要经过下面三个流程：</p> 
<ul><li>Authentication（认证）：身份鉴别，只有正确的账号才能够通过认证</li><li>Authorization（授权）： 判断用户是否有权限对访问的资源执行特定的动作</li><li>Admission Control（准入控制）：用于补充授权机制以实现更加精细的访问控制功能。</li></ul> 
<p><img src="https://i-blog.csdnimg.cn/blog_migrate/c88d9565ed3065270716940e0e66c788.png#pic_center" alt=""></p> 
<h3><a id="_78"></a>认证管理</h3> 
<p>Kubernetes集群安全的最关键点在于如何识别并认证客户端身份，它提供了3种客户端身份认证方式：</p> 
<ul><li>HTTP Base认证：通过用户名+密码的方式认证</li></ul> 
<blockquote> 
 <p>这种认证方式是把“用户名:密码”用BASE64算法进行编码后的字符串放在HTTP请求中的Header Authorization域里发送给服务端。服务端收到后进行解码，获取用户名及密码，然后进行用户身份认证的过程。</p> 
</blockquote> 
<ul><li>HTTP Token认证：通过一个Token来识别合法用户</li></ul> 
<blockquote> 
 <p>这种认证方式是用一个很长的难以被模仿的字符串–Token来表明客户身份的一种方式。每个Token对应一个用户名，当客户端发起API调用请求时，需要在HTTP Header里放入Token，API Server接到Token后会跟服务器中保存的token进行比对，然后进行用户身份认证的过程。</p> 
</blockquote> 
<ul><li>HTTPS证书认证：基于CA根证书签名的双向数字证书认证方式</li></ul> 
<blockquote> 
 <p>这种认证方式是安全性最高的一种方式，但是同时也是操作起来最麻烦的一种方式。</p> 
</blockquote> 
<p><img src="https://i-blog.csdnimg.cn/blog_migrate/c1f6dd9c0cb602c52e88078b627e00a2.png#pic_center" alt=""></p> 
<p><strong>HTTPS认证大体分为3个过程：</strong></p> 
<ol><li> <p>证书申请和下发</p> <pre><code class="prism language-markdown">  HTTPS通信双方的服务器向CA机构申请证书，CA机构下发根证书、服务端证书及私钥给申请者
</code></pre> </li><li> <p>客户端和服务端的双向认证</p> <pre><code class="prism language-markdown">  1&gt; 客户端向服务器端发起请求，服务端下发自己的证书给客户端，
     客户端接收到证书后，通过私钥解密证书，在证书中获得服务端的公钥，
     客户端利用服务器端的公钥认证证书中的信息，如果一致，则认可这个服务器
  2&gt; 客户端发送自己的证书给服务器端，服务端接收到证书后，通过私钥解密证书，
     在证书中获得客户端的公钥，并用该公钥认证证书信息，确认客户端是否合法
</code></pre> </li><li> <p>服务器端和客户端进行通信</p> <pre><code class="prism language-markdown">  服务器端和客户端协商好加密方案后，客户端会产生一个随机的秘钥并加密，然后发送到服务器端。
  服务器端接收这个秘钥后，双方接下来通信的所有内容都通过该随机秘钥加密
</code></pre> </li></ol> 
<blockquote> 
 <p>注意: Kubernetes允许同时配置多种认证方式，只要其中任意一个方式认证通过即可</p> 
</blockquote> 
<h3><a id="_124"></a>授权管理</h3> 
<pre><code>   授权发生在认证成功之后，通过认证就可以知道请求用户是谁， 然后Kubernetes会根据事先定义的授权策略来决定用户是否有权限访问，这个过程就称为授权。
  
   每个发送到ApiServer的请求都带上了用户和资源的信息：比如发送请求的用户、请求的路径、请求的动作等，授权就是根据这些信息和授权策略进行比较，如果符合策略，则认为授权通过，否则会返回错误。
</code></pre> 
<p>API Server目前支持以下几种授权策略：</p> 
<ul><li> <p>AlwaysDeny：表示拒绝所有请求，一般用于测试</p> </li><li> <p>AlwaysAllow：允许接收所有请求，相当于集群不需要授权流程（Kubernetes默认的策略）</p> </li><li> <p>ABAC：基于属性的访问控制，表示使用用户配置的授权规则对用户请求进行匹配和控制</p> </li><li> <p>Webhook：通过调用外部REST服务对用户进行授权</p> </li><li> <p>Node：是一种专用模式，用于对kubelet发出的请求进行访问控制</p> </li><li> <p>RBAC：基于角色的访问控制（kubeadm安装方式下的默认选项）</p> </li></ul> 
<p>RBAC(Role-Based Access Control) 基于角色的访问控制，主要是在描述一件事情：<strong>给哪些对象授予了哪些权限</strong></p> 
<p>其中涉及到了下面几个概念：</p> 
<ul><li>对象：User、Groups、ServiceAccount</li><li>角色：代表着一组定义在资源上的可操作动作(权限)的集合</li><li>绑定：将定义好的角色跟用户绑定在一起</li></ul> 
<p><img src="https://i-blog.csdnimg.cn/blog_migrate/940cf774d054b9f9916002765de70916.png#pic_center" alt="">]</p> 
<p>RBAC引入了4个顶级资源对象：</p> 
<ul><li>Role、ClusterRole：角色，用于指定一组权限</li><li>RoleBinding、ClusterRoleBinding：角色绑定，用于将角色（权限）赋予给对象</li></ul> 
<p><strong>Role、ClusterRole</strong></p> 
<p>一个角色就是一组权限的集合，这里的权限都是许可形式的（白名单）。</p> 
<pre><code class="prism language-yaml"><span class="token comment"># Role只能对命名空间内的资源进行授权，需要指定nameapce</span>
<span class="token key atrule">kind</span><span class="token punctuation">:</span> Role
<span class="token key atrule">apiVersion</span><span class="token punctuation">:</span> rbac.authorization.k8s.io/v1
<span class="token key atrule">metadata</span><span class="token punctuation">:</span>
<span class="token key atrule">namespace</span><span class="token punctuation">:</span> dev
<span class="token key atrule">name</span><span class="token punctuation">:</span> authorization<span class="token punctuation">-</span>role
<span class="token key atrule">rules</span><span class="token punctuation">:</span>
<span class="token punctuation">-</span> <span class="token key atrule">apiGroups</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token string">""</span><span class="token punctuation">]</span>  <span class="token comment"># 支持的API组列表,"" 空字符串，表示核心API群</span>
<span class="token key atrule">resources</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token string">"pods"</span><span class="token punctuation">]</span> <span class="token comment"># 支持的资源对象列表</span>
<span class="token key atrule">verbs</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token string">"get"</span><span class="token punctuation">,</span> <span class="token string">"watch"</span><span class="token punctuation">,</span> <span class="token string">"list"</span><span class="token punctuation">]</span> <span class="token comment"># 允许的对资源对象的操作方法列表</span>
</code></pre> 
<pre><code class="prism language-yaml"><span class="token comment"># ClusterRole可以对集群范围内资源、跨namespaces的范围资源、非资源类型进行授权</span>
<span class="token key atrule">kind</span><span class="token punctuation">:</span> ClusterRole
<span class="token key atrule">apiVersion</span><span class="token punctuation">:</span> rbac.authorization.k8s.io/v1
<span class="token key atrule">metadata</span><span class="token punctuation">:</span>
<span class="token key atrule">name</span><span class="token punctuation">:</span> authorization<span class="token punctuation">-</span>clusterrole
<span class="token key atrule">rules</span><span class="token punctuation">:</span>
<span class="token punctuation">-</span> <span class="token key atrule">apiGroups</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token string">""</span><span class="token punctuation">]</span>
<span class="token key atrule">resources</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token string">"pods"</span><span class="token punctuation">]</span>
<span class="token key atrule">verbs</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token string">"get"</span><span class="token punctuation">,</span> <span class="token string">"watch"</span><span class="token punctuation">,</span> <span class="token string">"list"</span><span class="token punctuation">]</span>
</code></pre> 
<p>需要详细说明的是，rules中的参数：</p> 
<ul><li> <p>apiGroups: 支持的API组列表</p> <pre><code class="prism language-bash"><span class="token string">""</span>,<span class="token string">"apps"</span>, <span class="token string">"autoscaling"</span>, <span class="token string">"batch"</span>
</code></pre> </li><li> <p>resources：支持的资源对象列表</p> <pre><code class="prism language-bash"><span class="token string">"services"</span>, <span class="token string">"endpoints"</span>, <span class="token string">"pods"</span>,<span class="token string">"secrets"</span>,<span class="token string">"configmaps"</span>,<span class="token string">"crontabs"</span>,<span class="token string">"deployments"</span>,<span class="token string">"jobs"</span>,
<span class="token string">"nodes"</span>,<span class="token string">"rolebindings"</span>,<span class="token string">"clusterroles"</span>,<span class="token string">"daemonsets"</span>,<span class="token string">"replicasets"</span>,<span class="token string">"statefulsets"</span>,
<span class="token string">"horizontalpodautoscalers"</span>,<span class="token string">"replicationcontrollers"</span>,<span class="token string">"cronjobs"</span>
</code></pre> </li><li> <p>verbs：对资源对象的操作方法列表</p> <pre><code class="prism language-bash"><span class="token string">"get"</span>, <span class="token string">"list"</span>, <span class="token string">"watch"</span>, <span class="token string">"create"</span>, <span class="token string">"update"</span>, <span class="token string">"patch"</span>, <span class="token string">"delete"</span>, <span class="token string">"exec"</span>
</code></pre> </li></ul> 
<p><strong>RoleBinding、ClusterRoleBinding</strong></p> 
<p>角色绑定用来把一个角色绑定到一个目标对象上，绑定目标可以是User、Group或者ServiceAccount。</p> 
<pre><code class="prism language-yaml"><span class="token comment"># RoleBinding可以将同一namespace中的subject绑定到某个Role下，则此subject即具有该Role定义的权限</span>
<span class="token key atrule">kind</span><span class="token punctuation">:</span> RoleBinding
<span class="token key atrule">apiVersion</span><span class="token punctuation">:</span> rbac.authorization.k8s.io/v1beta1
<span class="token key atrule">metadata</span><span class="token punctuation">:</span>
<span class="token key atrule">name</span><span class="token punctuation">:</span> authorization<span class="token punctuation">-</span>role<span class="token punctuation">-</span>binding
<span class="token key atrule">namespace</span><span class="token punctuation">:</span> dev
<span class="token key atrule">subjects</span><span class="token punctuation">:</span>
<span class="token punctuation">-</span> <span class="token key atrule">kind</span><span class="token punctuation">:</span> User
<span class="token key atrule">name</span><span class="token punctuation">:</span> Young
<span class="token key atrule">apiGroup</span><span class="token punctuation">:</span> rbac.authorization.k8s.io
<span class="token key atrule">roleRef</span><span class="token punctuation">:</span>
<span class="token key atrule">kind</span><span class="token punctuation">:</span> Role
<span class="token key atrule">name</span><span class="token punctuation">:</span> authorization<span class="token punctuation">-</span>role
<span class="token key atrule">apiGroup</span><span class="token punctuation">:</span> rbac.authorization.k8s.io
</code></pre> 
<pre><code class="prism language-yaml"><span class="token comment"># ClusterRoleBinding在整个集群级别和所有namespaces将特定的subject与ClusterRole绑定，授予权限</span>
<span class="token key atrule">kind</span><span class="token punctuation">:</span> ClusterRoleBinding
<span class="token key atrule">apiVersion</span><span class="token punctuation">:</span> rbac.authorization.k8s.io/v1beta1
<span class="token key atrule">metadata</span><span class="token punctuation">:</span>
<span class="token key atrule">name</span><span class="token punctuation">:</span> authorization<span class="token punctuation">-</span>clusterrole<span class="token punctuation">-</span>binding
<span class="token key atrule">subjects</span><span class="token punctuation">:</span>
<span class="token punctuation">-</span> <span class="token key atrule">kind</span><span class="token punctuation">:</span> User
<span class="token key atrule">name</span><span class="token punctuation">:</span> heima
<span class="token key atrule">apiGroup</span><span class="token punctuation">:</span> rbac.authorization.k8s.io
<span class="token key atrule">roleRef</span><span class="token punctuation">:</span>
<span class="token key atrule">kind</span><span class="token punctuation">:</span> ClusterRole
<span class="token key atrule">name</span><span class="token punctuation">:</span> authorization<span class="token punctuation">-</span>clusterrole
<span class="token key atrule">apiGroup</span><span class="token punctuation">:</span> rbac.authorization.k8s.io
</code></pre> 
<p><strong>RoleBinding引用ClusterRole进行授权</strong></p> 
<p>RoleBinding可以引用ClusterRole，对属于同一命名空间内ClusterRole定义的资源主体进行授权。</p> 
<blockquote> 
 <p>一种很常用的做法就是，集群管理员为集群范围预定义好一组角色（ClusterRole），然后在多个命名空间中重复使用这些ClusterRole。这样可以大幅提高授权管理工作效率，也使得各个命名空间下的基础性授权规则与使用体验保持一致。</p> 
</blockquote> 
<pre><code class="prism language-yaml"><span class="token comment"># 虽然authorization-clusterrole是一个集群角色，但是因为使用了RoleBinding</span>
<span class="token comment"># 所以Young只能读取dev命名空间中的资源</span>
<span class="token key atrule">kind</span><span class="token punctuation">:</span> RoleBinding
<span class="token key atrule">apiVersion</span><span class="token punctuation">:</span> rbac.authorization.k8s.io/v1beta1
<span class="token key atrule">metadata</span><span class="token punctuation">:</span>
<span class="token key atrule">name</span><span class="token punctuation">:</span> authorization<span class="token punctuation">-</span>role<span class="token punctuation">-</span>binding<span class="token punctuation">-</span>ns
<span class="token key atrule">namespace</span><span class="token punctuation">:</span> dev
<span class="token key atrule">subjects</span><span class="token punctuation">:</span>
<span class="token punctuation">-</span> <span class="token key atrule">kind</span><span class="token punctuation">:</span> User
<span class="token key atrule">name</span><span class="token punctuation">:</span> Young
<span class="token key atrule">apiGroup</span><span class="token punctuation">:</span> rbac.authorization.k8s.io
<span class="token key atrule">roleRef</span><span class="token punctuation">:</span>
<span class="token key atrule">kind</span><span class="token punctuation">:</span> ClusterRole
<span class="token key atrule">name</span><span class="token punctuation">:</span> authorization<span class="token punctuation">-</span>clusterrole
<span class="token key atrule">apiGroup</span><span class="token punctuation">:</span> rbac.authorization.k8s.io
</code></pre> 
<h4><a id="devPods_273"></a><strong>实战：创建一个只能管理dev空间下Pods资源的账号</strong></h4> 
<pre><code>1) 创建账号
</code></pre> 
<pre><code class="prism language-powershell"><span class="token comment"># 1) 创建证书</span>
<span class="token namespace">[root@master pki]</span><span class="token comment"># cd /etc/kubernetes/pki/</span>
<span class="token namespace">[root@master pki]</span><span class="token comment"># (umask 077;openssl genrsa -out devman.key 2048)</span>

<span class="token comment"># 2) 用apiserver的证书去签署</span>
<span class="token comment"># 2-1) 签名申请，申请的用户是devman,组是devgroup</span>
<span class="token namespace">[root@master pki]</span><span class="token comment"># openssl req -new -key devman.key -out devman.csr -subj "/CN=devman/O=devgroup"     </span>
<span class="token comment"># 2-2) 签署证书</span>
<span class="token namespace">[root@master pki]</span><span class="token comment"># openssl x509 -req -in devman.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out devman.crt -days 3650</span>

<span class="token comment"># 3) 设置集群、用户、上下文信息</span>
<span class="token namespace">[root@master pki]</span><span class="token comment"># kubectl config set-cluster kubernetes --embed-certs=true --certificate-authority=/etc/kubernetes/pki/ca.crt --server=https://192.168.109.100:6443</span>

<span class="token namespace">[root@master pki]</span><span class="token comment"># kubectl config set-credentials devman --embed-certs=true --client-certificate=/etc/kubernetes/pki/devman.crt --client-key=/etc/kubernetes/pki/devman.key</span>

<span class="token namespace">[root@master pki]</span><span class="token comment"># kubectl config set-context devman@kubernetes --cluster=kubernetes --user=devman</span>

<span class="token comment"># 切换账户到devman</span>
<span class="token namespace">[root@master pki]</span><span class="token comment"># kubectl config use-context devman@kubernetes</span>
Switched to context <span class="token string">"devman@kubernetes"</span><span class="token punctuation">.</span>

<span class="token comment"># 查看dev下pod，发现没有权限</span>
<span class="token namespace">[root@master pki]</span><span class="token comment"># kubectl get pods -n dev</span>
Error <span class="token keyword">from</span> server <span class="token punctuation">(</span>Forbidden<span class="token punctuation">)</span>: pods is forbidden: User <span class="token string">"devman"</span> cannot list resource <span class="token string">"pods"</span> in API <span class="token function">group</span> <span class="token string">""</span> in the namespace <span class="token string">"dev"</span>

<span class="token comment"># 切换到admin账户</span>
<span class="token namespace">[root@master pki]</span><span class="token comment"># kubectl config use-context kubernetes-admin@kubernetes</span>
Switched to context <span class="token string">"kubernetes-admin@kubernetes"</span><span class="token punctuation">.</span>
</code></pre> 
<p>2） 创建Role和RoleBinding，为devman用户授权</p> 
<pre><code class="prism language-yaml"><span class="token key atrule">kind</span><span class="token punctuation">:</span> Role
<span class="token key atrule">apiVersion</span><span class="token punctuation">:</span> rbac.authorization.k8s.io/v1beta1
<span class="token key atrule">metadata</span><span class="token punctuation">:</span>
<span class="token key atrule">namespace</span><span class="token punctuation">:</span> dev
<span class="token key atrule">name</span><span class="token punctuation">:</span> dev<span class="token punctuation">-</span>role
<span class="token key atrule">rules</span><span class="token punctuation">:</span>
<span class="token punctuation">-</span> <span class="token key atrule">apiGroups</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token string">""</span><span class="token punctuation">]</span>
<span class="token key atrule">resources</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token string">"pods"</span><span class="token punctuation">]</span>
<span class="token key atrule">verbs</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token string">"get"</span><span class="token punctuation">,</span> <span class="token string">"watch"</span><span class="token punctuation">,</span> <span class="token string">"list"</span><span class="token punctuation">]</span>

<span class="token punctuation">---</span>

<span class="token key atrule">kind</span><span class="token punctuation">:</span> RoleBinding
<span class="token key atrule">apiVersion</span><span class="token punctuation">:</span> rbac.authorization.k8s.io/v1beta1
<span class="token key atrule">metadata</span><span class="token punctuation">:</span>
<span class="token key atrule">name</span><span class="token punctuation">:</span> authorization<span class="token punctuation">-</span>role<span class="token punctuation">-</span>binding
<span class="token key atrule">namespace</span><span class="token punctuation">:</span> dev
<span class="token key atrule">subjects</span><span class="token punctuation">:</span>
<span class="token punctuation">-</span> <span class="token key atrule">kind</span><span class="token punctuation">:</span> User
<span class="token key atrule">name</span><span class="token punctuation">:</span> devman
<span class="token key atrule">apiGroup</span><span class="token punctuation">:</span> rbac.authorization.k8s.io
<span class="token key atrule">roleRef</span><span class="token punctuation">:</span>
<span class="token key atrule">kind</span><span class="token punctuation">:</span> Role
<span class="token key atrule">name</span><span class="token punctuation">:</span> dev<span class="token punctuation">-</span>role
<span class="token key atrule">apiGroup</span><span class="token punctuation">:</span> rbac.authorization.k8s.io
</code></pre> 
<pre><code class="prism language-powershell"><span class="token namespace">[root@master pki]</span><span class="token comment"># kubectl create -f dev-role.yaml</span>
role<span class="token punctuation">.</span>rbac<span class="token punctuation">.</span>authorization<span class="token punctuation">.</span>k8s<span class="token punctuation">.</span>io/dev-role created
rolebinding<span class="token punctuation">.</span>rbac<span class="token punctuation">.</span>authorization<span class="token punctuation">.</span>k8s<span class="token punctuation">.</span>io/authorization-role-binding created
</code></pre> 
<pre><code>3) 切换账户，再次验证
</code></pre> 
<pre><code class="prism language-powershell"><span class="token comment"># 切换账户到devman</span>
<span class="token namespace">[root@master pki]</span><span class="token comment"># kubectl config use-context devman@kubernetes</span>
Switched to context <span class="token string">"devman@kubernetes"</span><span class="token punctuation">.</span>

<span class="token comment"># 再次查看</span>
<span class="token namespace">[root@master pki]</span><span class="token comment"># kubectl get pods -n dev</span>
NAME                                 READY   STATUS             RESTARTS   AGE
nginx-deployment-66cb59b984-8wp2k    1/1     Running            0          4d1h
nginx-deployment-66cb59b984-dc46j    1/1     Running            0          4d1h
nginx-deployment-66cb59b984-thfck    1/1     Running            0          4d1h

<span class="token comment"># 为了不影响后面的学习,切回admin账户</span>
<span class="token namespace">[root@master pki]</span><span class="token comment"># kubectl config use-context kubernetes-admin@kubernetes</span>
Switched to context <span class="token string">"kubernetes-admin@kubernetes"</span><span class="token punctuation">.</span>
</code></pre> 
<h3><a id="_363"></a>准入控制</h3> 
<p>通过了前面的认证和授权之后，还需要经过准入控制处理通过之后，apiserver才会处理这个请求。</p> 
<p>准入控制是一个可配置的控制器列表，可以通过在Api-Server上通过命令行设置选择执行哪些准入控制器：</p> 
<pre><code>--admission-control=NamespaceLifecycle,LimitRanger,ServiceAccount,PersistentVolumeLabel,
                     DefaultStorageClass,ResourceQuota,DefaultTolerationSeconds
</code></pre> 
<p>只有当所有的准入控制器都检查通过之后，apiserver才执行该请求，否则返回拒绝。</p> 
<p>当前可配置的Admission Control准入控制如下：</p> 
<ul><li>AlwaysAdmit：允许所有请求</li><li>AlwaysDeny：禁止所有请求，一般用于测试</li><li>AlwaysPullImages：在启动容器之前总去下载镜像</li><li>DenyExecOnPrivileged：它会拦截所有想在Privileged Container上执行命令的请求</li><li>ImagePolicyWebhook：这个插件将允许后端的一个Webhook程序来完成admission controller的功能。</li><li>Service Account：实现ServiceAccount实现了自动化</li><li>SecurityContextDeny：这个插件将使用SecurityContext的Pod中的定义全部失效</li><li>ResourceQuota：用于资源配额管理目的，观察所有请求，确保在namespace上的配额不会超标</li><li>LimitRanger：用于资源限制管理，作用于namespace上，确保对Pod进行资源限制</li><li>InitialResources：为未设置资源请求与限制的Pod，根据其镜像的历史资源的使用情况进行设置</li><li>NamespaceLifecycle：如果尝试在一个不存在的namespace中创建资源对象，则该创建请求将被拒绝。当删除一个namespace时，系统将会删除该namespace中所有对象。</li><li>DefaultStorageClass：为了实现共享存储的动态供应，为未指定StorageClass或PV的PVC尝试匹配默认的StorageClass，尽可能减少用户在申请PVC时所需了解的后端存储细节</li><li>DefaultTolerationSeconds：这个插件为那些没有设置forgiveness tolerations并具有notready:NoExecute和unreachable:NoExecute两种taints的Pod设置默认的“容忍”时间，为5min</li><li>PodSecurityPolicy：这个插件用于在创建或修改Pod时决定是否根据Pod的security context和可用的PodSecurityPolicy对Pod的安全策略进行控制</li></ul>
                </div>
            </body>
            </html>
            